package com.example.demo;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class SelectDivice extends Activity {
	private GridView selectDivice;
	protected Myapplication app;
	private Socket sc;
	private Protocol protocol = new Protocol();
	private String status = new String();
	private String progress = "1";
	private String UserName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_divice);
		selectDivice = (GridView) findViewById(R.id.selectDivice);
		selectDivice.setAdapter(new ImageAdapter(this));
		app = (Myapplication) getApplication();
		sc = app.getSocket();
		Bundle receiveBundle = this.getIntent().getExtras();
		UserName = receiveBundle.getString("UserName");
		
		selectDivice.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:// light device

					try {
						protocol.sendMessages(sc, "LightDevice",
								Protocol.USER_SELECT);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					}
				//	String st = new String();

					// try {
				//	try {
					//	st = protocol.Receive(sc);
					//	if (protocol.findTypeMsg(st) == Protocol.USER_STATUS) {
					//		status = protocol.extractData(st);
						//	protocol.sendMessages(sc, "",
						//			Protocol.USER_STATUS_ACK);

						//}
					//	Log.i("status",status);
					
					//} catch (IOException e) {
						// TODO Auto-generated catch block
				//		e.printStackTrace();
				//	}

			
				//	Bundle sendBundle = new Bundle();
				//	sendBundle.putString("status", status);
				//	sendBundle.putString("progress", progress);
					Intent i = new Intent(SelectDivice.this, ListViewActivity.class);
				//	i.putExtras(sendBundle);
					startActivity(i);

					break;
				default :
						Intent j = new Intent(SelectDivice.this, ComingSoon.class);
						startActivity(j);
					break;
					

				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_select_divice, menu);
		return true;
	}
}
